package de.cityquest;

import java.util.List;

import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class Start extends MapActivity {
	
	MapView mMapView;
	MapController mMapController;
	
	GeoPoint mGeoPoint = new GeoPoint((int)(49.0785227 * 1E6), (int)(8.3882139 * 1E6));
	private LocationManager mLocationManager;
	private MyLocationOverlay mMyOverlay;
	
	@Override
	protected void onPause() {
		mMyOverlay.disableMyLocation();
		mMapView.getOverlays().remove(mMyOverlay);
		mLocationManager.removeUpdates(mMyOverlay);
		super.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();
		mLocationManager.requestLocationUpdates(
				LocationManager.GPS_PROVIDER, 
				5000L, 
				500.0f, 
				mMyOverlay);
		
		mMapView.getOverlays().add(mMyOverlay);
		mMyOverlay.enableMyLocation();
		
		mMyOverlay.runOnFirstFix(new Runnable() {
			
			@Override
			public void run() {
				mMapController.animateTo(mMyOverlay.getMyLocation());
			}
		});
	}

	@Override
	protected void onCreate(Bundle icicle) {
		
		super.onCreate(icicle);
		
		setContentView(R.layout.main);
		setTitle("City Quest");
		mMapView = (MapView) findViewById(R.id.mapview);
		mMapView.setBuiltInZoomControls(true);
		mMapView.setSatellite(true);
		
		mMapController = mMapView.getController();
//		mMapController.setCenter(mGeoPoint);
		mMapController.setZoom(18);
		
		mMyOverlay = new MyLocationOverlay(this, mMapView) {
			
			@Override
			public synchronized void onLocationChanged(Location location) {
				super.onLocationChanged(location);
				final GeoPoint meinePosition = new GeoPoint(
						(int) (location.getLatitude() * 1E6), 
						(int) (location.getLongitude() * 1E6)
				);
//				mMapController.animateTo(meinePosition);
				float[] results = new float[3];
				Location.distanceBetween(meinePosition.getLatitudeE6()/1E6, meinePosition.getLongitudeE6()/1E6, mGeoPoint.getLatitudeE6()/1E6, mGeoPoint.getLongitudeE6()/1E6, results);
//				float dist = results[0];
			}
			
		};
		mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
		
		//OverlayItems
		List<Overlay> mapOverlays = mMapView.getOverlays();
		Drawable drawable = getResources().getDrawable(R.drawable.androidmarker);
		OverlayItems itemizedoverlay = new OverlayItems(drawable, this);		
		
		//Pyramide OverlayItem
		OverlayItem overlayitem = new OverlayItem(mGeoPoint, "D1=", "D2=");
		itemizedoverlay.addOverlay(overlayitem);
		mapOverlays.add(itemizedoverlay);
		
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

}
